<script setup>
import useWebsiteStore from "@/store/useWebsiteStore";
import { inject, ref, watch } from "vue";

const { dialogVisible, setDialogVisible } = inject("dialog-visible");
// const {urlRef} = inject("dialog-url");
const urlRef = ref("");
const { addWebsite } = useWebsiteStore();
const isDisabledRef = ref(false);
watch(dialogVisible, () => {
  urlRef.value = "";
})

const handleAdd = async () => {
  isDisabledRef.value = true;
  const websiteInfo = await CONTEXTAPI.sendURL(urlRef.value);
  if (websiteInfo.hasOwnProperty("msg")) {
    CONTEXTAPI.alert(websiteInfo.msg);
  } else {
    addWebsite(websiteInfo);
    setDialogVisible(false);
  };
  isDisabledRef.value = false;
}

</script>
<template>
  <teleport to="body">
    <div class="dialog-wrap">
      <div class="content">
        <div class="input">
          <input v-model="urlRef" @keyup.enter="handleAdd" type="text" placeholder="请输入网址..." :disabled="isDisabledRef">
        </div>
        <div class="btns">
          <button @click="handleAdd" :disabled="isDisabledRef">添加</button>
          <button @click="setDialogVisible(false)" :disabled="isDisabledRef">取消</button>
        </div>
      </div>
    </div>
  </teleport>
</template>
<style lang="scss" scoped>
.dialog-wrap {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  .content {
    width: 100%;
    padding: 0 20px;
    input {
      width: 100%;
      height: 30px;
      margin-bottom: 10px;
      font-size: 12px;
      outline: none;
    }
    .btns {
      button {
        height: 30px;
        margin-right: 10px;
        padding: 0 20px;
        font-size: 12px;
      }
    }
  }
}
</style>